﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    public class ScreenCallBackDal
    {
        public DapperHelper dapperHelper { get; set; }


        /// <summary>
        /// 根据任务ID获取筛查通知信息
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public ScreenNoticeView GetScreenNoticeData(Guid taskId, Guid ownerId)
        {
            if (taskId != Guid.Empty && ownerId != Guid.Empty)
            {
                return new DapperHelper().ExecueWithoutTransaction(select =>
                {
                    string strsql = @"select task.ActivityId as taskId,screen.new_screenId as screenId,
                                      screen.new_name as screenName,task.subject as taskName,
                                      task.scheduledstart as screenStartData,
                                      task.scheduledend as taskEndData,
                                      case screen.new_screenstate when 0 
                                      then '新建' when 1 then '发布' when 2 then '撤销' when 4 then '暂停' when 5 then '进行中' when 3 then '已完成' else '无选择' end as 
                                      taskStatus,
                                      screen.new_startdate as screenStartData,
                                      (community.new_provinceName+community.new_cityName+community.new_countyName+community.new_streetName+community.new_name) as screenAddress,
                                      screen.new_checkinaddress as checkinAddress,screen.new_screenteam as screenTeamId
                                      from task
                                      left join new_screenBase screen
                                      on task.regardingobjectid = screen.new_screenid
                                      left join new_community community on screen.new_community=community.new_communityId
                                      /**where**/";
                    SqlBuilder sqlBuilder = new SqlBuilder();
                    var sqlTemplate = sqlBuilder.AddTemplate(strsql);
                    sqlBuilder.Where("task.ActivityId = @taskId", new { taskId = taskId });
                    sqlBuilder.Where("task.OwnerId = @ownerId", new { ownerId = ownerId });
                    return select.QueryFirstOrDefault<ScreenNoticeView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
                });
            }
            return null;
        }

        public IEnumerable<ScreenCallBackView> GetScreenCallBacksByHospID(Guid accountId)
        {

            return dapperHelper.ExecueWithoutTransaction<IEnumerable<ScreenCallBackView>>(a =>
            {
                string sql = @"select * from AIER_SCREENCALLBACK_INFO /**where**/";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where("new_account = @accountId", new { accountId = accountId });
                //sqlBuilder.AddParameters(new { siteid= hospitalId });


                return a.Query<ScreenCallBackView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });


        }

        /// <summary>
        /// 根据任务ID与筛查宣教ID获取筛查回访结果
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public ScreenCallBackView GetScreenCallBacksByID(Guid taskId,Guid screenId)
        {
            return dapperHelper.ExecueWithoutTransaction<ScreenCallBackView>(a =>
            {
                string sql = @"select top 1 new_screencallbackid,new_communicateresult,new_contactresult,new_description 
                from new_screencallbackBase /**where**/ /**orderby**/";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where("new_task = @taskId", new { taskId = taskId });
                sqlBuilder.Where("new_screen = @screenId", new { screenId = screenId });
                sqlBuilder.OrderBy("createdon desc");
                return a.QueryFirstOrDefault<ScreenCallBackView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });
        }

        /// <summary>
        /// 根据潜客Id获取筛查回访信息
        /// </summary>
        /// <param name="leadId"></param>
        /// <returns></returns>
        public IEnumerable<dynamic> GetScreenCallBackByLeadId(Guid leadId)
        {
            if (leadId != Guid.Empty)
            {
                return new DapperHelper().ExecueWithoutTransaction(select =>
                {
                    string strsql = @"select new_screencallbackId,OwnerIdName,createdon, 
                                        case new_contactresult 
                                        when 100000000 then '已联系'
                                        when 100000001 then '未联系上'
                                        when 100000002 then '稍后联系'
                                        when 100000003 then '拒绝再联系'
                                        when 100000004 then '空号'
                                        when 100000005 then '停机'
                                        else '无数据' end as new_contactresult,
                                        case new_communicateresult
                                        when 100000000 then '形成预约'
                                        when 100000001 then '无意向'
                                        when 100000002 then '犹豫'
                                        when 100000003 then '他院预约或就医'
                                        else '无数据' end as new_communicateresult
                                        from new_screencallback 
                                        /**where**/ /**orderby**/";
                    SqlBuilder sqlBuilder = new SqlBuilder();
                    var sqlTemplate = sqlBuilder.AddTemplate(strsql);
                    sqlBuilder.Where("new_lead = @leadId", new { leadId = leadId });
                    sqlBuilder.OrderBy("CreatedOn desc ");
                    return select.Query<dynamic>(sqlTemplate.RawSql, sqlTemplate.Parameters);
                });
            }
            return null;
        }
    }
}
